package com.yince.demo.clockcard.slice;

import com.yince.demo.clockcard.ResourceTable;
import com.yince.demo.clockcard.utils.ComponentProviderUtils;
import com.yince.demo.clockcard.utils.DateUtils;
import com.yince.demo.clockcard.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Clock Card Slice
 */
public class ClockCardSlice extends AbilitySlice {
    private static final HiLogLabel LABEL_Log = new HiLogLabel(0, 0, ClockCardSlice.class.getName());
    private static final Long SEND_PERIOD = 1000L;
    private static final int COLOR_RGB = 192;
    private static final int TIME_LENGTH = 2;
    private Text dateText;
    private Text hourText;
    private Text minuteText;
    private Text secondText;
    private EventRunner runner;
    private MyEventHandle myEventHandle;
    private AbilitySlice slice = this;
    private Timer timer;
    private Runnable runnable = new Runnable() {
        private void initHandler() {
            runner = EventRunner.getMainEventRunner();

            if (runner == null) {
                return;
            }
            myEventHandle = new MyEventHandle(runner);
        }

        @Override
        public void run() {
            initHandler();
        }
    };

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ResourceTable.Layout_form_image_with_info_date_card_2_2));
        initComponent();
        startTimer();
    }

    private void startTimer() {
        timer = new Timer();
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        runnable.run();
                        myEventHandle.sendEvent(1);
                    }
                },
                0,
                SEND_PERIOD
        );
    }


    private void initComponent() {
        Calendar now = Calendar.getInstance();

        Component dateComponet = slice.findComponentById(ResourceTable.Id_date);
        if (dateComponet != null && dateComponet instanceof Text) {
            dateText = (Text) dateComponet;
            dateText.setText(DateUtils.getCurrentDate("yyyy-MM-dd"));
        }

        Component hourComponent = slice.findComponentById(ResourceTable.Id_hour);
        if (hourComponent != null && hourComponent instanceof Text) {
            hourText = (Text) hourComponent;
            int hour = now.get(Calendar.HOUR_OF_DAY);
            setTextValue(hour, hourText);
        }

        Component minComponent = findComponentById(ResourceTable.Id_min);
        if (minComponent != null && minComponent instanceof Text) {
            minuteText = (Text) minComponent;
            int min = now.get(Calendar.MINUTE);
            setTextValue(min, minuteText);
        }
        Component secComponent = findComponentById(ResourceTable.Id_sec);
        if (secComponent != null && secComponent instanceof Text) {
            secondText = (Text) secComponent;
            int second = now.get(Calendar.SECOND);
            setTextValue(second, secondText);
        }
        int weekId = ComponentProviderUtils.getWeekDayId();
        Component weekComponent = slice.findComponentById(weekId);
        if (weekComponent != null && weekComponent instanceof Text) {
            Text week = (Text) weekComponent;
            week.setTextColor(new Color(Color.rgb(0, 0, 0)));
        }
        int lastWeekDayId = ComponentProviderUtils.getLastWeekDayId();
        Component lastWeekComponent = slice.findComponentById(lastWeekDayId);
        if (weekComponent != null && lastWeekComponent instanceof Text) {
            Text lastWeek = (Text) lastWeekComponent;
            lastWeek.setTextColor(new Color(Color.rgb(COLOR_RGB, COLOR_RGB, COLOR_RGB)));
        }
    }

    private void setTextValue(int now, Text text) {
        if (String.valueOf(now).length() < TIME_LENGTH) {
            text.setText("0" + now);
        } else {
            text.setText(now + "");
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private class MyEventHandle extends EventHandler{
        MyEventHandle(EventRunner runner) throws IllegalArgumentException{
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);

            int eventId=event.eventId;

            if (eventId==1){
                initComponent();
            }
        }
    }

    @Override
    protected void onStop() {
        LogUtils.info("onStop", " start to destroy slice");
        timer.cancel();
    }
}

